package org.example.thread;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.Random;
import java.util.concurrent.*;

public class CompleTableFutureDemo04 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService =
                new ThreadPoolExecutor(8,16,30000, TimeUnit.MILLISECONDS,
                        new LinkedBlockingQueue<>(100),new ThreadPoolExecutor.AbortPolicy());

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("future1 done...");
            }
            return "abc";
        },executorService);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("future2 done...");
            }
            return "efg";
        },executorService);
        //全部执行完毕后再执行后续内容
        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(future1, future2);
        //只有有一个结束了就执行后续内容
       // CompletableFuture<Object> completableFuture = CompletableFuture.anyOf(future1, future2);
        //合并
        completableFuture.join();
        System.out.println(completableFuture.isDone());
        System.out.println("all futures done...");
        executorService.shutdown();

    }
}
